import org.w3c.dom.ls.LSException;

import java.nio.file.NotDirectoryException;
import java.util.*;

public class BinaryTree {
    static class TreeNode {
        public TreeNode left;
        public TreeNode right;
        public char val;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode root;
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;

        B.left = D;
        B.right = E;
        E.right = H;
        C.left = F;
        C.right = G;
        return  A;
    }
    public void preOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    public void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);

    }
    public void postOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    public int NodeSize;
    // 获取树中节点的个数
   public int size(TreeNode root) {
        if(root == null) {
            return 0;
        }
       size(root.right);
       size(root.left);
        NodeSize++;
        return NodeSize;
   }
   public int size2(TreeNode root) {
       if(root == null) {
           return 0;
       }
           return size2(root.left) + size2(root.right) +1;


   }
   public int size;
    // 获取叶子节点的个数
    public int getLeafNodeCount(TreeNode root) {
       if(root == null) {
           return 0;
       }
       if(root.left == null && root.right == null) {
           size++;
       }
       getLeafNodeCount(root.left);
       getLeafNodeCount(root.right);
       return size;
    }
    public int getLeafNodeCount2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }
    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root,int k) {
        if(root == null) {
            return  0 ;
        }
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1)+
               getKLevelNodeCount(root.right , k-1);
    }
    // 获取二叉树的高度
    public int getHeight(TreeNode root) {
        if(root == null ) {
            return 0;
        }
        int l = getHeight(root.left) ;
        int r = getHeight(root.right);
        return  (l>r?l:r) + 1;
    }
    // 检测值为value的元素是否存在
     public TreeNode find(TreeNode root, int val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        TreeNode left = find(root.left,val);
        if(left != null) {
            return left;
        }
        TreeNode right = find(root.right,val);
        if(right != null) {
            return right;
        }
        return null;
     }


    /*
判断一棵树是不是完全二叉树
*/
    public boolean isCompleteTree(TreeNode root) {
        if(root == null) {
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            } else {
                break;
            }
        }
            while(!queue.isEmpty()) {
              TreeNode cur = queue.poll();
              if(cur != null) {
                  return  false;
              }
            }
        return  true;
    }
    /*
    二叉树遍历
    import java.util.Scanner;
class TreeNode {
    public char val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(char val) {
       this.val = val;
    }
    public TreeNode() {
        ;
    }

}
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
           String str = in.nextLine();
           TreeNode root = createTree(str);
           inOrder(root);
        }
    }
    public static int i = 0;
    private static TreeNode createTree(String str) {
        TreeNode root = null;
        char ch = str.charAt(i);
        if(ch != '#') {
            root = new TreeNode(ch);
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        } else {
            i++;
        }
        return root;

    }
    private static  void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

}
     */

    //层序遍历
    public void levelOrder1(TreeNode root) {
        if(root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while( !queue.isEmpty()) {
            TreeNode top = queue.poll();
            System.out.print(top.val + " ");
            if(top.left != null) {
                queue.offer(top.left);
            }
            if(top.right != null) {
                queue.offer((top.right));
            }
        }
    }

    public List<List<Character>> levelOrder2(TreeNode root) {
        List<List<Character>> retList = new ArrayList<>();
        if(root == null) {
            return retList;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while( !queue.isEmpty()) {
            int size = queue.size();
            List<Character> list = new ArrayList<>();
            while(size != 0) {
                TreeNode top = queue.poll();
                list.add(top.val);
                size--;
                if(top.left != null) {
                    queue.offer(top.left);
                }
                if(top.right != null) {
                    queue.offer((top.right));
                }
            }
            retList.add(list);
        }
        return retList;
    }

}
